/**
 * 格式化时间
 * @param {*} val  单位s
 * @returns 
 */
function fmtTime(val = 0) {
    let hour = Math.floor(val / 3600);
    let minute = Math.floor(val / 60 % 60);
    let second = Math.floor(val % 60);
    let fmtHour = hour > 9 ? hour : `0${hour}`;
    let fmtMinute = minute > 9 ? minute : `0${minute}`;
    let fmtSecond = second > 9 ? second : `0${second}`;

    if(hour > 0) {
        return `${fmtHour}:${fmtMinute}:${fmtSecond}`;
    }else {
        return `${fmtMinute}:${fmtSecond}`; 
    }
}

/**
 * 格式化时间
 * @param {*} val 单位s
 * @returns 
 */
function fmtTransTime(val = 0) {
    if(val == 0) {
        return '0秒';
    }
    let hour = Math.floor(val / 3600);
    let minute = Math.floor(val / 60 % 60);
    let second = Math.floor(val % 60);
    let fmtHour = hour > 0 ? `${hour}小时` : '';
    let fmtMinute = minute > 0 ? `${minute}分钟` : '';
    let fmtSecond = second > 0 ? `${second}秒` : '';

    return fmtHour + fmtMinute + fmtSecond;
}

/**
 * 格式化学段学科
 * @param {
 * } dataList 
 * @param {*} nodata 没有内容的默认显示文本
 * @returns 
 */
const handleStageSubject = (dataList, nodata = '') => {
    let stageList = dataList || [];
    let str = '';
    stageList.forEach((stageItem) => {
        let grades = stageItem.subjectList
            .map((item) => {
                return item.subjectName;
            })
            .join('，');
        str += `${stageItem.stageName}：${grades}` + '；';
    });
    str = str.substring(0, str.length - 1);
    return str || nodata;
}


/**
 * 格式化年级
 * @param {
 * } dataList 
 * @param {*} nodata 没有内容的默认显示文本
 * @returns 
 */
const handleGrade = (dataList, nodata = '') => {
    let stageList = dataList || [];
    let str = '';
    stageList.forEach((stageItem) => {
        if (stageItem.gradeList && stageItem.gradeList.length) {
            let grades = stageItem.gradeList
                .map((item) => {
                    return item.gradeName;
                })
                .join('；');
            str += `${grades}` +'：';
        } 
    });
    str = str.substring(0, str.length - 1);
    return str || nodata;
}

// 格式化单独的学段学科列表
const handleAloneStageSubject = (list, emptyStr) => {
    let stageStr = '';
    // >小学-语文；初中-数学；高中-思想品德
    (list || []).map(item => {
        stageStr += ' [' + item.stageName; 
        if(item.subjectList && item.subjectList.length) {
            stageStr += '：';
            item.subjectList.map((el,index) => {
                if(index == item.subjectList.length - 1){
                    stageStr +=  el.subjectName + '] '
                }else{
                    stageStr +=  el.subjectName + ','
                }
            })
        } else {
            stageStr += ']';
        }
    })
    return stageStr ? stageStr : emptyStr ? emptyStr : '';
}

// 格式化单独的年级列表
const handleAloneGrade = (list, emptyStr) => {
    let gradeStr = '';
    gradeStr = (list || []).map(item => {
        return item.gradeName
    }).join(', ');
    return gradeStr ? gradeStr : emptyStr ? emptyStr : '';
}


export {
    fmtTime,
    handleStageSubject,
    fmtTransTime,
    handleGrade,
    handleAloneStageSubject,
    handleAloneGrade
};
